//
//  SoftwareInfo.m
//  gdemm
//
//  Created by 乔明超 on 16/6/28.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "SoftwareInfo.h"
#import "KeyChain.h"
#import "PackageInfo.h"
#import "ApiSupport.h"
#import "HttpUtil.h"
#import <objc/runtime.h>
@interface SoftwareInfo()
@property(strong,nonatomic)NSMutableArray *mPacks;
@property(strong,nonatomic)NSMutableArray *mAppList;
@end

@implementation SoftwareInfo

//懒加载

- (NSMutableArray *)mPacks{
    if (_mPacks == nil) {
        _mPacks = [NSMutableArray array];
    }
    
    return _mPacks;
}
- (NSMutableArray *)mAppList{
    if (_mAppList == nil) {
        _mAppList = [NSMutableArray array];
    }
    return _mAppList;
}
static SoftwareInfo *mInstance;
+ (instancetype)getInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mInstance = [[self alloc]init];
    });
    return mInstance;
}


- (void)parseResponse:(NSDictionary *)dict{
    NSString *result = dict[@"result"];
    if ([result isEqualToString:@"success"]) {
        
    }else{
        NSString *errCode = dict[@"errCode"];
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        if ([errCode isEqualToString:@"1"]) {
            [window makeToast:@"未传入序列号"];
        }else if ([errCode isEqualToString:@"2"]){
            [window makeToast:@"软件列表为空"];
        }else if ([errCode isEqualToString:@"3"]){
            [window makeToast:@"设备不存在"];
        }else if ([errCode isEqualToString:@"4"]){
            [window makeToast:@"参数错误"];
        }else if ([errCode isEqualToString:@"9"]){
            [window makeToast:@"SQL错误"];
        }
        
    }
}
/**
 *  软件信息提交
 *
 */
- (void)commitSoftInfo{

    @synchronized (self) {
        NSMutableDictionary *obj = [NSMutableDictionary dictionary];
        NSString *serial = [KeyChain getUDID];
        [obj setValue:serial forKey:@"serial"];
        [self.mPacks removeAllObjects];
        [self.mAppList removeAllObjects];
        [self getAllAppsFromDevice];
        for (PackageInfo *packageInfo in self.mPacks) {
            NSMutableDictionary *appDict = [NSMutableDictionary dictionary];
            [appDict setValue:packageInfo.softName forKey:@"softName"];
            [appDict setValue:@0 forKey:@"size"];
            [appDict setValue:packageInfo.version forKey:@"version"];
            [appDict setValue:packageInfo.packageName forKey:@"packageName"];
            [self.mAppList addObject:appDict];
        }
        
        [obj setValue:self.mAppList forKey:@"appList"];
        NSDictionary *result = [HttpUtil postWithUrl:[ApiSupport getSoftwareUrl] withDict:obj withType:@"softInfo"];
        [self parseResponse:result];
    }
    
}

- (void)getAllAppsFromDevice
{

    //获取手机上所有的app
    Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
    NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
    NSArray *apps = [workspace performSelector:@selector(allInstalledApplications)];
    Class LSApplicationProxy_class = objc_getClass("LSApplicationProxy");
    for (int i = 0; i < apps.count; i++) {
        NSObject *temp = apps[i];
        if ([temp isKindOfClass:LSApplicationProxy_class]) {
            NSString *appBundleId = [temp performSelector:NSSelectorFromString(@"applicationIdentifier")];
            NSString *appName = [temp performSelector:NSSelectorFromString(@"localizedName")];
            
            NSString *appVersion = [temp performSelector:NSSelectorFromString(@"shortVersionString")];
            NSInteger appSize = [temp performSelector:NSSelectorFromString(@"description")];
            NSString * type = [temp performSelector:NSSelectorFromString(@"applicationType")];
            
            if ([type isEqualToString:@"User"]) {
                PackageInfo *packageInfo = [[PackageInfo alloc]init];
                packageInfo.softName = appName;
                packageInfo.packageName = appBundleId;
                packageInfo.version = appVersion;
                packageInfo.size = 0;
                [self.mPacks addObject:packageInfo];
            }
            
        }
    }
    
}

@end
